package anoaop.lanjie;

import anoaop.service.UserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
@Service
@Aspect
public class LogLanjie {
    @Around("within(anoaop.service..*)")
    //@Around("within(anoaop.service.UserService.del)")
    public Object doLanjie(ProceedingJoinPoint jp) throws Throwable {
        //
        long start = System.nanoTime();
        System.out.println("方法：" + jp.getTarget() + "." + jp.getSignature().getName() + "在什么时刻执行？" + new Date());
        System.out.println("参数：" + Arrays.toString(jp.getArgs()));
        //不考虑del有重载的情况
        if (jp.getTarget().getClass() == UserService.class && jp.getSignature().getName().equals("del")) {
            Integer id = (Integer)jp.getArgs()[0];
            if(id==1){
                throw  new RuntimeException("id是1的user不能删除");
            }
        }
        Object result = jp.proceed();
        long end = System.nanoTime();
        System.out.println(jp.getSignature().getName() + " 返回了：" + result + ":" + "耗时" + (end - start) + "纳秒");
        return result;
    }
}
